home *** CD-ROM | disk | FTP | other *** search
- Path: engr.orst.edu!usenet
- From: Sreekanth Nagarajan <nagarasr@research.cs.orst.edu>
- Newsgroups: comp.lang.c++
- Subject: Re: A beginners valiant effort.
- Date: Sun, 18 Feb 1996 17:22:11 -0800
- Organization: College of Engineering, Oregon State University
- Message-ID: <3127D0C3.54B@research.cs.orst.edu>
- References: <4g81kr$mrs@soap.news.pipex.net>
- NNTP-Posting-Host: ada.cs.orst.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.05 9000/730)
- CC: neales@dial.pipex.com
-
- I am not sure what is exactly your problem. But your code seems to have
- couple of problems.
-
-
- 1. Since get_string() takes a 'boolean' it is wise to pass a boolean
- value (true or false) while calling it. Many c++ compilers won't even
- compile the given code (atleast mine didn't) since you are trying to
- pass int in place of boolean.
-
- 2. You have defined the string_val_xxx arrays to be of fixed size. In
- the given example, you are passing a 16 character string as
- string_val_one and it is exactly of 16-char size. But to store this
- string properly (with a terminating '\0') you need atleast 17
- characters.
-
- The program when executed on my machine produced the following output:
-
- 1is not floweringis flowering
-
- This is understandable. Here is what might have happened:
-
- In set_string(), when string1 is copied to string_val_one, there is no
- space to put the terminating null. Thus it spills over to the first
- character of string_val_two. (Normally two variables defined next to
- each other are allocated space in that order, by many compilers) This
- null is overwritten when second string is copied. Again your
- string_val_two also has 1-char less space for your string. But then the
- strcpy goes ahead and puts the null beyond the last character of
- string_val_two. Depending on your environ, this may work or may lead to
- a crash!
-
- When getstring executes, it sees that string_index is 1 and properly
- executes the true part of IF. This part prints your string_val_one.
- Since string_val_two immediately follows string_val_one and there is no
- terminating null in string_val_one, the contents of both are printed.
-
- Hope things are clear now :)
-
- The rule is: if you are statically allocating space, allocate space
- larger than the longest possible string that you expect. This way you
- can avoid surprises (and possible crashes!)
-
- Happy C++-ing..
-
- Sreekanth
- nagarasr@cs.orst.edu
-
-
- > please e-mail responses to neales@dial.pipex.com - Ta.
- >
- > #include <iostream.h>
- > #include <string.h>
- >
- > enum boolean{false, true};
- >
- > class switc_string
- > {
- > private:
- > char string_val_one[16];
- > char string_val_two[12];
- > public:
- > void set_string(char string1[], char string2[])
- > {
- > strcpy(string_val_one, string1);
- > strcpy(string_val_two, string2);
- > }
- > void get_string(boolean string_index)
- > {
- > cout << string_index;
- > if ( string_index )
- > {cout << string_val_one;}
- >
- > else
- > {cout << string_val_two;}
- > }
- > };
- >
- > void main()
- >
- > {
- >
- > switc_string s1;
- > s1.set_string("is not flowering","is flowering");
- > cout << endl;
- > s1.get_string(1);
- >
- > }
-